<?php
namespace store;

use utils\PathTool;
use utils\StringTool;

/*
*/
class FileBlockWriter
{
	var $m_pathSvr;
	
	function __construct() 
	{
	}

	//创建文件
	function make(/*FileInf*/$file)
	{   
		//创建层级目录
	    $fd = dirname($file->pathSvr);
		//if( !file_exists($fd) && !is_dir($fd)) mkdir($fd,0777,true);
		PathTool::mkdir($fd);

		$hfile = fopen($file->pathSvr,"wb");
		ftruncate($hfile,$file->lenLoc);
		fclose($hfile);
		
		if(!file_exists($file->pathSvr)) throw new \Exception("make file error");
		return "";
	}
	
	/*
		写入文件块数据
	*/
	function write(/*FileInf*/$file,$data)
	{
	    //写入数据
	    $hfile = fopen($file->pathSvr,"r+b");
	    //定位到续传位置
	    fseek($hfile, intval($file->blockOffset),SEEK_SET);
	    fwrite($hfile,$data);
	    fclose($hfile);
	    
	    return $file->pathSvr;
	}
}
?>